<template>
  <body-index>
    <body-left title="我的消息" slot="bodyLeft">
      <template slot="main">
        <div v-if="list.length === 0">
          <el-empty
            description="还没有消息哦"
            :image-size="200"
            :image="require(`bodyLeftDefaultImage`)"
          ></el-empty>
        </div>
        <div v-else class="container-fluid">
          <div class="row">
            <div v-if="isAdmin" class="col-12 padding-10">
              <el-button @click="dialog = true" type="primary" size="small"
                >发布社区通知</el-button
              >
              <el-dialog
                modal-append-to-body
                append-to-body
                title="创作文章"
                width="80%"
                :visible.sync="dialog"
              >
                <el-input
                  v-model="noticeContent"
                  placeholder="输入内容"
                ></el-input>
                <span slot="footer" class="dialog-footer">
                  <el-button
                    @click="
                      dialog = false;
                      noticeContent = '';
                    "
                    >取 消</el-button
                  >
                  <el-button
                    type="primary"
                    @click="
                      noticeAll();
                      dialog = false;
                      noticeContent = '';
                    "
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
            </div>
            <div
              class="col-12 border-bottom padding-20"
              v-for="(item, position) in list"
              :key="item.id"
            >
              <div class="row">
                <div class="col-12 padding-5">
                  <template v-if="item.type === 'msg'">
                    <span>来自 </span>
                    <strong @click="visitUser(item.from)" id="user">{{
                      getFrom(item.from).nick
                    }}</strong>
                    <el-tooltip
                      effect="dark"
                      content="管理员"
                      placement="right"
                    >
                      <i
                        v-if="false"
                        alt="管理员"
                        style="color: gold; font-size: 15px; font-weight: bold"
                        class="iconfont icon-renzheng"
                      ></i>
                    </el-tooltip>
                    <span> 的消息</span>
                  </template>

                  <template v-if="item.type === 'notice'">
                    <strong>社区通知</strong>
                  </template>
                  <span id="time">&nbsp;&nbsp;&nbsp;&nbsp;{{ item.time }}</span>
                </div>

                <div class="col-12 padding-5">
                  <span style="float: left">{{ item.content }}</span>

                  <div style="display: inline-block; float: right">
                    <el-popover
                      v-if="item.from !== null"
                      trigger="hover"
                      placement="top"
                      width="160"
                      :value="showReplay === position"
                    >
                      <p>
                        <el-input
                          v-model.trim="replayContent"
                          :placeholder="
                            ' 回复 ' + getWriter(item.from).nick + ' :'
                          "
                        ></el-input>
                      </p>
                      <div style="text-align: right; margin: 0">
                        <el-button
                          size="mini"
                          type="text"
                          @click="
                            showReplay = '';
                            replayContent = '';
                          "
                          >取消</el-button
                        >
                        <el-button
                          type="primary"
                          size="mini"
                          @click="
                            replay(item.id);
                            showReplay = '';
                            replayContent = '';
                          "
                          >确定</el-button
                        >
                      </div>
                      <el-button
                        v-if="item.type === 'msg'"
                        @click="showReplay = position"
                        type="text"
                        slot="reference"
                        ><span
                          class="icon"
                          style="color: #41b883; cursor: pointer"
                          ><i class="el-icon-chat-dot-round"></i> 回复
                        </span></el-button
                      >
                    </el-popover>
                    <el-popconfirm
                      @confirm="deleteMsg(item.id, position)"
                      title="确定删除这条消息吗？"
                    >
                      <el-button slot="reference" type="text"
                        ><span style="color: red"
                          >&nbsp;&nbsp;&nbsp;&nbsp;删除</span
                        ></el-button
                      >
                    </el-popconfirm>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </body-left>
  </body-index>
</template>

<script>
import BodyIndex from "../../pages/body/BodyIndex.vue";
import BodyLeft from "../../pages/body/BodyLeft.vue";
import { fake } from "../../assets/fake";
import Admin from "../mode/Admin.vue";
export default {
  components: { BodyIndex, BodyLeft, Admin },
  data() {
    return {
      isAdmin: false,
      list: [],
      replayContent: "",
      showReplay: "",
      dialog: false,
      noticeContent: "",
    };
  },
  mounted() {
    if (fake.self !== null) {
      this.isAdmin = fake.self.isAdmin;
      this.list = fake.methods.getMsg(fake.self.info.id);
    }
  },
  methods: {
    getFrom(id) {
      return fake.methods.getUserInfo(id);
    },
    visitUser(id) {
      this.$router.push({
        name: "User",
        query: {
          id: id,
        },
      });
    },
    replay(to) {
      fake.methods.sendMsg(fake.self.info.id, to, this.replayContent, "msg");
      this.$notify({
        title: "提示",
        message: "回复成功，等待对方查收",
        type: "success",
      });
    },
    deleteMsg(id, index) {
      fake.methods.deleteMsg(fake.self.info.id, id);
      this.list.splice(index, 1);
      this.$notify({
        title: "提示",
        message: "已删除该消息",
        type: "success",
      });
    },
    getWriter(writerId) {
      writerId = writerId + "";
      return fake.methods.getUserInfo(writerId);
    },
    noticeAll() {
      if (fake.self.isAdmin) {
        fake.methods.noticeAll(this.noticeContent);
        this.$notify({
          title: "成功发送",
          message: "已发送全体通知",
          type: "success",
        });
      } else {
        this.$notify({
          title: "错误",
          message: "非管理员无法操作",
          type: "warning",
        });
      }
    },
  },
};
</script>

<style scoped>
#time {
  color: rgba(0, 0, 0, 0.4);
  font-size: 14px;
}
strong {
  font-size: 16px;
}
#user {
  transition: all 0.2s;
}
#user:hover {
  color: #41b883;
  cursor: pointer;
  transition: all 0.2s;
}
</style>